Skip to content

Allow to configure reflection prompt#636

Closed
LeoVS09 wants to merge 2 commits intoMervinPraison:mainfrom
LeoVS09:feature/add-reflection-prompt
Closed

Allow to configure reflection prompt#636
LeoVS09 wants to merge 2 commits intoMervinPraison:mainfrom
LeoVS09:feature/add-reflection-prompt

Conversation

@LeoVS09
Copy link
Copy Markdown
Contributor

@LeoVS09 LeoVS09 commented Jun 10, 2025

User description

I currently struggle to get any use from the self-reflection feature. During reflection, LLM always reports that the response is satisfactory. I want to provide clear requirements for reflection through the additional parameter reflect_prompt.

This change was useful in my own project, so it will probably be useful for someone else.


PR Type

Enhancement


Description

• Add configurable reflect_prompt parameter to Agent class
• Replace hardcoded reflection prompt with customizable option
• Enable users to provide specific reflection requirements
• Maintain backward compatibility with default prompt fallback


Changes walkthrough 📝

Relevant files
Enhancement
agent.py
Add configurable reflection prompt parameter                         

src/praisonai-agents/praisonaiagents/agent/agent.py

• Added reflect_prompt parameter to Agent init method
• Stored
reflect_prompt as instance variable
• Modified reflection logic to use
custom prompt when provided
• Added fallback to default prompt for
backward compatibility

+3/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Summary by CodeRabbit

    • New Features
      • Added support for customizing the reflection prompt used during agent self-reflection in chat interactions. If no custom prompt is provided, the default prompt will be used.

    @coderabbitai
    Copy link
    Copy Markdown
    Contributor

    coderabbitai bot commented Jun 10, 2025

    Walkthrough

    The Agent class in agent.py was updated to accept an optional reflect_prompt parameter during initialization. This parameter customizes the reflection prompt used in the agent's self-reflection loop within the chat method, defaulting to the original prompt if not provided. No other logic was changed.

    Changes

    File(s) Change Summary
    src/praisonai-agents/praisonaiagents/agent/agent.py Added optional reflect_prompt parameter to Agent.__init__ with detailed docstring; used it in chat to customize reflection prompt

    Sequence Diagram(s)

    sequenceDiagram
        participant User
        participant Agent
    
        User->>Agent: Initialize Agent(reflect_prompt=custom?)
        User->>Agent: chat()
        Agent->>Agent: Generate response
        Agent->>Agent: Self-reflection loop
        Agent->>Agent: Use reflect_prompt (custom or default)
        Agent-->>User: Return response
    
    Loading

    Poem

    A bunny with prompts so bright,
    Now reflects with words you write.
    Custom thoughts in every chat,
    Hopping smarter—fancy that!
    With each new prompt, it leaps ahead,
    Tail a-wiggle, ears widespread.
    🐇✨


    📜 Recent review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL
    Plan: Pro

    📥 Commits

    Reviewing files that changed from the base of the PR and between 70043cb and c19216e.

    📒 Files selected for processing (1)
    • src/praisonai-agents/praisonaiagents/agent/agent.py (3 hunks)
    🚧 Files skipped from review as they are similar to previous changes (1)
    • src/praisonai-agents/praisonaiagents/agent/agent.py
    ✨ Finishing Touches
    • 📝 Generate Docstrings

    Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

    ❤️ Share
    🪧 Tips

    Chat

    There are 3 ways to chat with CodeRabbit:

    • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
      • I pushed a fix in commit <commit_id>, please review it.
      • Explain this complex logic.
      • Open a follow-up GitHub issue for this discussion.
    • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
      • @coderabbitai explain this code block.
      • @coderabbitai modularize this function.
    • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
      • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
      • @coderabbitai read src/utils.ts and explain its main purpose.
      • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
      • @coderabbitai help me debug CodeRabbit configuration file.

    Support

    Need help? Create a ticket on our support page for assistance with any issues or questions.

    Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

    CodeRabbit Commands (Invoked using PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai full review to do a full review from scratch and review all the files again.
    • @coderabbitai summary to regenerate the summary of the PR.
    • @coderabbitai generate docstrings to generate docstrings for this PR.
    • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
    • @coderabbitai help to get help.

    Other keywords and placeholders

    • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
    • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
    • Add @coderabbitai anywhere in the PR title to generate the title automatically.

    CodeRabbit Configuration File (.coderabbit.yaml)

    • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
    • Please see the configuration documentation for more information.
    • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

    Documentation and Community

    • Visit our Documentation for detailed information on how to use CodeRabbit.
    • Join our Discord Community to get help, request features, and share feedback.
    • Follow us on X/Twitter for updates and announcements.

    Copy link
    Copy Markdown
    Contributor

    @gemini-code-assist gemini-code-assist bot left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Summary of Changes

    Hello @LeoVS09, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

    This pull request introduces the ability to customize the prompt used by the agent for its self-reflection step. This enhancement addresses the issue where the default reflection prompt might consistently yield 'satisfactory' results, enabling users to guide the reflection process with more specific requirements tailored to their use case.

    Highlights

    • Agent Configuration: Added an optional reflect_prompt parameter to the Agent constructor, allowing users to provide a custom prompt for the self-reflection process.
    • Reflection Logic: Modified the internal reflection prompt generation within the chat method to use the user-provided reflect_prompt if available, falling back to the default reflection instructions otherwise.
    Using Gemini Code Assist

    The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

    Invoking Gemini

    You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

    Feature Command Description
    Code Review /gemini review Performs a code review for the current pull request in its current state.
    Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
    Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
    Help /gemini help Displays a list of available commands.

    Customization

    To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

    Limitations & Feedback

    Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

    You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

    Footnotes

    1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

    @qodo-code-review
    Copy link
    Copy Markdown

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Missing Validation

    The reflect_prompt parameter is not validated for type or content, which could lead to runtime errors if non-string values are passed or if the prompt format is incompatible with the reflection logic.

    reflect_prompt: Optional[str] = None,
    user_id: Optional[str] = None,
    Documentation Gap

    The new reflect_prompt parameter lacks docstring documentation explaining its purpose, expected format, and usage examples, which could make it difficult for users to understand how to use this feature effectively.

    reflect_prompt: Optional[str] = None,
    user_id: Optional[str] = None,

    @qodo-code-review
    Copy link
    Copy Markdown

    qodo-code-review bot commented Jun 10, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Fix prompt template integration

    The custom reflection prompt should be properly integrated into the template
    structure. Currently, it's inserted as a standalone line which may break the
    prompt flow and formatting.

    src/praisonai-agents/praisonaiagents/agent/agent.py [1233-1238]

     reflection_prompt = f"""
     Reflect on your previous response: '{response_text}'.
    -{self.reflect_prompt if self.reflect_prompt else "Identify any flaws, improvements, or actions."}
    +{self.reflect_prompt or "Identify any flaws, improvements, or actions."}
     Provide a "satisfactory" status ('yes' or 'no').
     Output MUST be JSON with 'reflection' and 'satisfactory'.
                         """
    • Apply / Chat
    Suggestion importance[1-10]: 2

    __

    Why: The suggestion proposes changing if self.reflect_prompt else to use the or operator. While functionally equivalent in this context, using or for default values is a common Python idiom and can be considered slightly more concise and readable. The impact is minimal and purely stylistic.

    Low
    • Update

    Copy link
    Copy Markdown
    Contributor

    @gemini-code-assist gemini-code-assist bot left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Code Review

    This pull request introduces a configurable reflect_prompt parameter to the Agent class, allowing users to customize the reflection instructions for the self-reflection feature. The changes correctly add the new parameter to the __init__ method, store it as an instance variable, and utilize it in the synchronous chat method's reflection logic, falling back to a default prompt if the custom one isn't provided. This successfully addresses the user's goal of providing clearer requirements for reflection.

    The implementation within the provided diffs is clear and follows Python best practices (PEP 8).

    However, there are a few areas that need attention:

    • Inconsistent Feature Implementation: The new reflect_prompt is used in the chat method, but the asynchronous achat method (specifically around line 1403-1408 in the full file context) still uses the hardcoded reflection prompt. This means the custom reflection prompt feature will not work for asynchronous chat operations, leading to inconsistent behavior. It's important to update the achat method to also use self.reflect_prompt to ensure feature parity between synchronous and asynchronous operations. The existing TODO comment in achat about syncing with chat highlights this area.
    • Missing Documentation: The docstring for the Agent.__init__ method should be updated to include a description of the new reflect_prompt parameter, explaining its purpose and usage. This is important for maintainability and for users to understand how to use this new feature.
    • Lack of Unit Tests: To ensure the reliability and correctness of this new feature, unit tests should be added. These tests should cover scenarios such as:
      • Using the default prompt when reflect_prompt is None.
      • Using the custom prompt when reflect_prompt is provided.
      • Behavior when reflect_prompt is an empty string (should use the default).
      • Verification of the feature in both chat and achat methods (once achat is updated).

    Addressing these points will significantly improve the completeness and robustness of this enhancement.

    Copy link
    Copy Markdown
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Actionable comments posted: 0

    🧹 Nitpick comments (3)
    src/praisonai-agents/praisonaiagents/agent/agent.py (3)

    370-372: Introduce configurable reflection prompt
    The addition of reflect_prompt: Optional[str] = None next to reflect_llm correctly exposes the self-reflection prompt to users. To complete this feature, please update the class docstring (or README) to document the new parameter and its default behavior.


    473-475: Persist the custom prompt on the instance
    Assigning self.reflect_prompt = reflect_prompt aligns with the constructor change. Optionally, you may want to trim or validate the prompt (e.g., ensure it’s not just whitespace) before storing.


    1233-1236: Use dynamic prompt in reflection loop
    The f-string correctly injects self.reflect_prompt with a fallback to the original static text. To ensure this works as intended, consider adding unit tests that pass a custom reflect_prompt and verify it's used in the messages sent to the LLM.

    Would you like help drafting those tests?

    📜 Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL
    Plan: Pro

    📥 Commits

    Reviewing files that changed from the base of the PR and between a4be556 and 70043cb.

    📒 Files selected for processing (1)
    • src/praisonai-agents/praisonaiagents/agent/agent.py (3 hunks)

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @claude review above changes and make sure it’s all correct and fix or optimise if required in this pull request

    also minimal code change

    @claude
    Copy link
    Copy Markdown
    Contributor

    claude bot commented Jun 11, 2025

    Claude encountered an error —— View job

    Failed with exit code 128
    

    I'll analyze this and get back to you.

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @claude review above changes and make sure it’s all correct and fix or optimise if required in this pull request

    also minimal code change

    @claude
    Copy link
    Copy Markdown
    Contributor

    claude bot commented Jun 11, 2025

    Claude encountered an error —— View job

    Failed with exit code 128
    

    I'll analyze this and get back to you.

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @claude review above changes and make sure it’s all correct and fix or optimise if required

    also minimal code change

    @claude
    Copy link
    Copy Markdown
    Contributor

    claude bot commented Jun 12, 2025

    Claude encountered an error —— View job

    Failed with exit code 128
    

    I'll analyze this and get back to you.

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @claude review above changes and make sure it’s all correct and fix or optimise if required

    also minimal code change. Try it in a different branch

    @claude
    Copy link
    Copy Markdown
    Contributor

    claude bot commented Jun 12, 2025

    Claude encountered an error —— View job

    Failed with exit code 128
    

    I'll analyze this and get back to you.

    @LeoVS09
    Copy link
    Copy Markdown
    Contributor Author

    LeoVS09 commented Jun 12, 2025

    @MervinPraison, it appears that Claude cannot add a comment to the PR created from the fork. I can try to push the branch to your repo and recreate the PR. But unfortunately, I do not have access to do it.

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @LeoVS09 Thanks for your contribution. Continuing this in here: #641

    If any issue persists, please create a new pull request or issue.

    I will try to get this feature published within a week.

    @MervinPraison
    Copy link
    Copy Markdown
    Owner

    @LeoVS09 Please upgrade and test it now.

    @LeoVS09
    Copy link
    Copy Markdown
    Contributor Author

    LeoVS09 commented Jun 22, 2025

    Thanks for merging changes @MervinPraison. I was busy at work this week. I will let you know once test it

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants